﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO; // 添加缺少的命名空间
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.esriSystem; // 添加缺少的命名空间
using ESRI.ArcGIS.DataSourcesFile; // 添加缺少的命名空间
using System.Runtime.InteropServices;

namespace MyGIS.Forms
{
    public partial class Save : Form
    {
        private AxMapControl mMapControl;
        private IFeatureLayer mFeatureLayer;


        public Save(AxMapControl mapControl)
        {
            InitializeComponent();
            this.mMapControl = mapControl;
            LoadLayersToComboBox();
        }

        private void LoadLayersToComboBox()
        {
            cboLayer.Items.Clear();
            for (int i = 0; i < mMapControl.LayerCount; i++)
            {
                ILayer layer = mMapControl.get_Layer(i);
                if (layer is IFeatureLayer) // 只添加要素图层
                {
                    cboLayer.Items.Add(layer.Name);
                }
            }

            if (cboLayer.Items.Count > 0)
            {
                cboLayer.SelectedIndex = 0;
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboLayer.SelectedIndex == -1) return;

            // 获取选中的图层
            mFeatureLayer = mMapControl.get_Layer(cboLayer.SelectedIndex) as IFeatureLayer;
        }

        private void Save_Load(object sender, EventArgs e)
        {
            // 可以在这里添加初始化代码
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (mFeatureLayer == null)
            {
                MessageBox.Show("请先选择要保存的图层！");
                return;
            }

            // 使用文件夹选择对话框让用户选择保存位置
            FolderBrowserDialog folderDialog = new FolderBrowserDialog();
            folderDialog.Description = "选择保存Shapefile的文件夹";

            if (folderDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            string folderPath = folderDialog.SelectedPath;
            string layerName = mFeatureLayer.Name;
            string fileName = $"{layerName}_{DateTime.Now:yyyyMMdd_HHmmss}.shp";
            string fullPath = Path.Combine(folderPath, fileName);

            try
            {
                // 打开目标工作空间
                IWorkspaceFactory shpWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
                IWorkspace shpWorkspace = shpWorkspaceFactory.OpenFromFile(folderPath, 0);

                // 获取源要素类
                IFeatureClass inputFeatureClass = mFeatureLayer.FeatureClass;
                IDataset inDataset = (IDataset)inputFeatureClass;
                IFeatureClassName inputFCName = (IFeatureClassName)((IDatasetName)inDataset.FullName);

                // 设置目标要素类名称
                IDataset outDataset = (IDataset)shpWorkspace;
                IWorkspaceName outWorkspaceName = (IWorkspaceName)outDataset.FullName;

                IDatasetName outDatasetName = new FeatureClassNameClass();
                outDatasetName.Name = Path.GetFileNameWithoutExtension(fileName);
                outDatasetName.WorkspaceName = outWorkspaceName;

                IFeatureClassName outputFCName = (IFeatureClassName)outDatasetName;

                // 执行转换
                IFeatureDataConverter converter = new FeatureDataConverterClass();
                converter.ConvertFeatureClass(
                    inputFCName, null, null,
                    outputFCName, null, null,
                    "", 1000, 0);

                MessageBox.Show($"图层已成功保存为Shapefile:\n{fullPath}", "导出成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"导出Shapefile时出错:\n{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}